﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;
using EntitySpaces.Interfaces;

public partial class Admin_FinLend_FinLend001c : AdminBasePage
{
    string ids = string.Empty;
    protected decimal? lendId;
    protected string companyName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["lendId"] != null)
        {
            lendId = decimal.Parse(Request.QueryString["lendId"]);
        }

        if (Request.QueryString["companyName"] != null)
        {
            companyName = Request.QueryString["companyName"].ToString();
        }

        if (Request.QueryString["ids"] != null)
        {
            ids = Request.QueryString["ids"].ToString();
        }
        if (lendId.HasValue && (!string.IsNullOrEmpty(ids)))
        {
            string storageLocation = string.Empty;
            FinLendReceiptQuery q1 = new FinLendReceiptQuery("q1");
            FinReceiptQuery q2=new FinReceiptQuery("q2");
            q1.Select(q2.StorageLocation).Where(q1.LendID==lendId.Value);
            q1.InnerJoin(q2).On(q1.ReceiptID==q2.ReceiptID);
            q1.es.Distinct=true;
            DataTable dt=q1.LoadDataTable();
            if (dt.Rows.Count>0)
            {
                storageLocation = dt.Rows[0]["StorageLocation"].ToString();
            }
                       
            ids = ids.Substring(0, ids.Length - 1);


            string[] idList = ids.Split(',');
            DateTime now = DateTime.Now;
            string currentUser = LoginUserInfo.CurrentUser.LoginName;
            using (esTransactionScope tr = new esTransactionScope())
            {
                q1 = new FinLendReceiptQuery("q1");
                q2=new FinReceiptQuery("q2");
                ArrayList argsOr = new ArrayList();
                for (int i = 0; i < idList.Length; i++)
                {
                    argsOr.Add(q1.ReceiptID==decimal.Parse(idList[i]));
                    q1.Select(q2.StorageLocation).Where(q1.Or(argsOr.ToArray()));
                    q1.InnerJoin(q2).On(q1.ReceiptID == q2.ReceiptID);
                    q1.es.Distinct = true;
                }
                dt = q1.LoadDataTable();
                if (dt.Rows.Count == 1)
                {
                    if (string.IsNullOrEmpty(storageLocation) &&( storageLocation == dt.Rows[0]["storageLocation"].ToString()))
                    {
                        for (int j = 0; j < idList.Length; j++)
                        {
                            FinLendReceipt finLendReceipt = new FinLendReceipt();
                            finLendReceipt.ReceiptID=decimal.Parse(idList[j]);
                            finLendReceipt.LendID=lendId;
                            finLendReceipt.CreatedBy = finLendReceipt.UpdatedBy = currentUser;
                            finLendReceipt.CreatedDttm=finLendReceipt.UpdatedDttm=now;
                            finLendReceipt.Save();
                            
                        }
                        FinLend finLend = new FinLend();
                        if (finLend.LoadByPrimaryKey(lendId.Value))
                        {
                            finLend.ThirdUnitID = decimal.Parse(dt.Rows[0]["storageLocation"].ToString());
                            finLend.Save();
                        }

                        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('借阅凭证挑选成功！');", true);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('选择借阅的凭证必须在同一个存储地！');", true);
                    }
                }
                else
                {
                    if (dt.Rows.Count > 1)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showNews('选择借阅的凭证必须在同一个存储地！');", true);
                    }
                }


                tr.Complete();
            }
        }
    }
}